<script>
  import { getMedicalDict } from '@/api/order';
//   import { foodProduceOnSiteCheckQuestionColumn, foodProduceOnSiteCheckQuestionDetailColumn } from '@/utils/column';
  import { foodProduction_getScoreSelect } from '@/api/order';
  import NormalTable from '@/components/normalTable/index';
  import { foodProduceOnSiteCheckQuestionDetailColumn, foodProduceOnSiteCheckQuestionColumn } from '@/utils/column'

  const map = new Map();
  const originMap = new Map();
export default {
  props: {
    list: Array,
    originList: Array,
    foodCategoryList: Array
  },
  components: {
    NormalTable
  },
  data(){
    return {
      dataSource: [],
      foodCheckTypeList: [],
      foodProduceOnSiteCheckQuestionColumn,
      foodProduceOnSiteCheckQuestionDetailColumn
    }
  },
  computed:{
    showCategory(){
      return key => {
        if(this.foodCategoryList.length == 0) return '';
        return this.foodCategoryList.find(item => item.value == key)?.label || ''
      }
    },
    formatter(){
      return (key, value) => {
        if(key == 'checkType'){
          if(this.foodCheckTypeList.length == 0) return;
          return this.foodCheckTypeList.find(item => item.value == value)?.label || ''
        }else return value
      }
    }
  },
  created(){
    this.getDictData();
  },
  methods: {
    // 获取字典
    getDictData() {
      getMedicalDict('food_check_type').then(res => {
        this.foodCheckTypeList = res.data
      });
    },

    async getList() {
      let addList = [];
      console.log(this,)
      this.originList.forEach((item, index) => {
        originMap.set(item.id, item);
        if(item.adjWorkOrderId){
          addList.push(item.adjWorkOrderId)
        }else {
          addList.push(item.id)
        }
      })
      let r = [];
      if(addList.length){
        r = (await foodProduction_getScoreSelect(addList)).data;
      }
      let arr = [...this.originList];
      this.dataSource = r.map(item => {
        map.set(item.workOrderId, item)
        return {
          ...originMap.get(item.workOrderId),
          ...item,
        }
      })
      let key = ['foodCategory', 'categoryName', 'categoryDetails', 'checkResult', 'standardLabel']
      arr = arr.map(item => {
        let obj = {};
        key.forEach(k => obj[k] = item[k])
        return {
          ...obj,
          adjWorkOrderId: item.id
        }
      })
      this.$emit('updateList', arr);
    }

  }
}
</script>

<template>
  <view class="flex-col flex gap-4">
    <a-card v-for="item in dataSource" :key="item.adjWorkOrderId">
      <view class="flex flex-col gap-4">
        <view>食品、食品添加剂类别及类别名称： {{ showCategory(item.foodCategory) }}-{{ showCategory(item.categoryName) }}</view>
        <uni-table border stripe>
          <uni-tr style="background-color: #e9f1fe">
            <uni-th v-for="(item,index) in foodProduceOnSiteCheckQuestionColumn" :key="item.key" :width="item.width" align="center">{{ item.title }}</uni-th>
          </uni-tr>
          <uni-tr v-for="(item,index) in item.score" :key="item[rowKey]">
            <uni-td align="center" v-for="child in foodProduceOnSiteCheckQuestionColumn" :key="child.key + item[rowKey]">{{ formatter(child.key,item[child.key]) }}</uni-td>
          </uni-tr>
        </uni-table>
        <view>核查得分率：{{ item.scoringRate }}；单项得分为0的共{{ item.scoreIsZero }}项</view>
        <uni-table border stripe>
          <uni-tr style="background-color: #e9f1fe">
            <uni-th v-for="(item,index) in foodProduceOnSiteCheckQuestionDetailColumn" :key="item.key" :width="item.width" align="center">{{ item.title }}</uni-th>
          </uni-tr>
          <uni-tr v-for="(item,index) in item.problem" :key="item[rowKey]">
            <uni-td align="center" v-for="child in foodProduceOnSiteCheckQuestionDetailColumn" :key="child.key + item[rowKey]">
              <view>{{ item[child.key] }}</view>
              <view v-if="child.key == 'serialNumber'">{{ item.isZero == 'true' ? '(零分项)' : '' }}</view>

            </uni-td>
          </uni-tr>
        </uni-table>
      </view>
    </a-card>
  </view>
</template>

